<?php

namespace app\airobot\controller;

use app\airobot\CommonLogic;
use app\airobot\model\OriginRobotSpin;
use app\airobot\model\RobotSpinConfig;
use app\airobot\model\UserModel;
use think\admin\Controller;

/**
 * 超管系统-Spin结果
 * Class UserSpin
 * @package app\airobot\controller
 */
class TestSpinResult extends Controller
{
    private $table = 'robot_spin_1';
    public $commonLogic;

    /**
     * Spin结果
     * @auth true
     */
    public function index()
    {
        $this->commonLogic = new CommonLogic();
        $this->title = 'Spin结果';
        $query = $this->_query($this->table);
        $where = [];
        if (!empty($_GET['batch_number'])) {
            $where['batch_number'] = $_GET['batch_number'];
        }
        $query->where($where);
        $query->order(['id' => 'desc'])->page();
    }

    /**
     * @throws \Exception
     */
    protected function _index_page_filter(array &$data)
    {
        $this->commonLogic = new CommonLogic();
        // 用户
        $userIds = array_column($data, 'user_id');
        $userModel = new UserModel();
        $userList = $userModel->queryList([], ['field' => 'id', 'data' => $userIds], [], [], [], "", $field = "*", $order = "");
        $userMap = array_column($userList, null, 'id');

        // 商品(从配置里面取)
        $batchNumberArr = [];
        foreach ($data as $vo) {
            $batchNumberArr[] = $vo['batch_number'];
        }
        $robotSpinConfigModel = new RobotSpinConfig();
        // 查询配置
        $configProductListMap = [];
        $robotSpinConfigList = $robotSpinConfigModel->queryList([], ['field' => 'batch_number', 'data' => $batchNumberArr], [], [], [], "", $field = "*", $order = "");
        foreach ($robotSpinConfigList as $config) {
            $files = explode("|", $config['config_file']);
            foreach ($files as $file) {
                // 文件名
                $fileArr = explode("/", $file);
                $fileNameArr = explode("-", $fileArr[count($fileArr) - 1]);
                $fileName = $fileNameArr[count($fileNameArr) - 1];
                if ($fileName == "product.csv") {
                    $configFileUpload = new ConfigFileUpload($this->app);
                    $pItemMap = $configFileUpload->importProduct($file, "", false);
                    $configProductListMap[$config['batch_number']] = $pItemMap;
                    break;
                }
            }
        }

        foreach ($data as &$vo) {
            $vo['username'] = isset($userMap[$vo['user_id']]) ? $userMap[$vo['user_id']]['username'] : '';
            $vo['product_name'] = "";
            if (isset($configProductListMap[$vo['batch_number']])) {
                $configProductMap = $configProductListMap[$vo['batch_number']];
                $vo['product_name'] = isset($configProductMap[$vo['product_id']]) ? $configProductMap[$vo['product_id']]['name'] : '';
            }
            if (strlen($vo['product_name']) > 30) {
                $vo['product_name'] = $this->commonLogic->TruncateString($vo['product_name'], 30) . "...";
            }
            if ($vo['status'] == 1) {
                $systemProfit = (float)$vo['spin_amount'];
            } else {
                $systemProfit = (float)$vo['spin_amount'] - (float)$vo['product_amount'];
            }
            $vo['system_profit'] = $this->commonLogic->DealAmount(floatval($systemProfit));
            $vo['spin_amount'] = $this->commonLogic->DealAmount($vo['spin_amount']);
            $vo['product_amount'] = $this->commonLogic->DealAmount($vo['product_amount']);
        }
    }

    /**
     * 查看参数
     * @auth true
     */
    public function param()
    {
        $userSpinModel = new \app\airobot\model\RobotSpin();
        $vo = $userSpinModel->queryInfo(["id" => $_GET["id"]]);

        // 将 JSON 字符串解析为 PHP 对象或数组
        $paramsJson = json_decode($vo['params_json'], true);
        $formattedJsonString = json_encode($paramsJson, JSON_PRETTY_PRINT);
        $vo['params_json'] = $formattedJsonString;

        $this->vo = $vo;
        OriginRobotSpin::mForm('param');
    }
}